﻿/*
 *  Sqlite ORM - PUBLIC DOMAIN LICENSE
 *  Copyright (C)  2010-2012. Ian Quigley
 *  
 *  This source code is provided 'As is'. You bear the risk of using it.
 */

using System.Linq;
using SqliteORM.Tests.Model;
using SubSpec;
using Xunit;

namespace SqliteORM.Tests
{
	public class Delete
	{
		[Specification]
		public void SingleEntryAndDelete()
		{
			"Given an empty database"
				.Context( ( ) =>
				          	{
				          		Context.Initialise();
				          		DbConnection.Initialise( Context.ConnectionString );
                                Context.DropTables();
				          	} );


			TableIdString test = new TableIdString() {Id = 1, Text = "Hello"};

			"when you create and delete a record".Do( 
                () => {
                        using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
			            {
			                adapter.CreateUpdate( test );
			            	adapter.Delete( test );
                        }
			           } );

            "expect that the row cannot be found".Assert(
                () =>
                {
                    using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
                        Assert.Null(adapter.Read(test.Id));                    
                });

            "expect that the table is empty".Assert(
                () =>
                {
                    using (TableAdapter<TableIdString> adapter = TableAdapter<TableIdString>.Open())
                        Assert.Equal(0, adapter.Select().Count());                               
                });
        }

        [Specification]
		public void SelectiveDeletingOfRecord()
		{
            TableParam known = new TableParam() { Id = 23 };

			"Given a single param-table with known value"
				.Context( ( ) =>
				          	{
				          		Context.Initialise();
				          		DbConnection.Initialise( Context.ConnectionString );

                                using (TableAdapter<TableParam> adapter = TableAdapter<TableParam>.Open(19))
                                {
                                    adapter.CreateUpdate(known);
                                }
				          	} );


            TableParam test = new TableParam() { Id = 1 };

			"when you create and delete another record".Do( 
                () => {
                    using (TableAdapter<TableParam> adapter = TableAdapter<TableParam>.Open(19))
			            {
			                adapter.CreateUpdate( test );
			            	adapter.Delete( test.Id );
                        }
			           } );

            "expect that the table known value still exists".Assert(
                () =>
                {
                    using (TableAdapter<TableParam> adapter = TableAdapter<TableParam>.Open(19))
                        Assert.NotNull(adapter.Read(known.Id));                    
                });

            "expect that the table test value doesn't exist".Assert(
                () =>
                {
                    using (TableAdapter<TableParam> adapter = TableAdapter<TableParam>.Open(19))
                        Assert.Null(adapter.Read(test.Id));                    
                });

            "expect that the table has only 1 value".Assert(
                () =>
                {
                    using (TableAdapter<TableParam> adapter = TableAdapter<TableParam>.Open(19))
                        Assert.Equal(1, adapter.Select().Count());                               
                });
        }

		[Specification]
		public void SelectiveDeletingWithTwoPrimaryKeys()
		{
			TableTwoPks known = new TableTwoPks() { IdOne = 23, IdTwo = 99, Text = "Hello" };
            
			"Given a table with two primary keys and some data "
				.Context(() =>
				{
					Context.Initialise();
					DbConnection.Initialise(Context.ConnectionString);

					using (TableAdapter<TableTwoPks> adapter = TableAdapter<TableTwoPks>.Open())
					{
						adapter.CreateUpdate(known);
					}
				});


			TableTwoPks test = new TableTwoPks() { IdOne = known.IdOne, IdTwo = known.IdTwo + 1, Text = "nice one"};

			"when you create and delete another record".Do(
				() =>
				{
					using (TableAdapter<TableTwoPks> adapter = TableAdapter<TableTwoPks>.Open())
					{
						adapter.CreateUpdate(test);
						adapter.Delete(test.IdOne, test.IdTwo);
					}
				});

			"expect that the table known value still exists".Assert(
				() =>
				{
					using (TableAdapter<TableTwoPks> adapter = TableAdapter<TableTwoPks>.Open())
						Assert.NotNull(adapter.Read(known.IdOne, known.IdTwo));
				});

			"expect that the table test value doesn't exist".Assert(
				() =>
				{
					using (TableAdapter<TableTwoPks> adapter = TableAdapter<TableTwoPks>.Open())
						Assert.Null(adapter.Read(test.IdOne, test.IdTwo));
				});

			"expect that the table has only 1 value".Assert(
				() =>
				{
					using (TableAdapter<TableTwoPks> adapter = TableAdapter<TableTwoPks>.Open())
						Assert.Equal(1, adapter.Select().Count());
				});
		}

		[Specification]
		public void DeleteAll()
		{
			"Given a table with data"
				.Context(() =>
				{
					Context.Initialise();
					DbConnection.Initialise(Context.ConnectionString);

					using (TableAdapter<TableParam> adapter = TableAdapter<TableParam>.Open(55))
					{
						adapter.CreateUpdate(1);
						adapter.CreateUpdate(2);
						adapter.CreateUpdate(3);
					}
				});


			"when you call Delete All".Do(
				() =>
				{
					using (TableAdapter<TableParam> adapter = TableAdapter<TableParam>.Open(55))
					{
						adapter.DeleteAll();
					}
				});

			"expect that the table is empty".Assert(
				() =>
				{
					using (TableAdapter<TableParam> adapter = TableAdapter<TableParam>.Open(55))
						Assert.Equal(0, adapter.Select().Count());
				});
		}


	}	

}
